//----------------------------------Include-------------------------------------
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include "mcu.h"
#include "board.h"
#include "glbvar.h"
#include "calc.h"
#include "dbg.h"
//------------------------------------------------------------------------------

//-----------------------------------Macro--------------------------------------
#define  BUF_N       8
//------------------------------------------------------------------------------

//---------------------------------Static Var-----------------------------------
static double        disp_rms_u;
static double        disp_rms_i;
static double        disp_pwr_p;
static double        disp_factor;
static double        disp_thd;
static double        disp_harmonic_amp[12];
//------------------------------------------------------------------------------
static double        buff_rms_u [BUF_N];
static double        buff_rms_i [BUF_N];
static double        buff_pwr_p [BUF_N];
static double        buff_factor[BUF_N];
static double        buff_thd   [BUF_N];
static double        buff_harmonic_amp[12][BUF_N];
//------------------------------------------------------------------------------

//--------------------------------Static Func-----------------------------------
#define ALPHA       0.8
static double _exp_move_avr(double* buf, uint32_t bufsz, double in)
{
  //move
  for(uint32_t i=0; i<bufsz-1; i++){ buf[i] = buf[i+1]; }
  buf[bufsz-1] = in;
  
  //calc
  double rt=0;
  for(uint32_t i=0; i<bufsz-1; i++){ rt += buf[i]; }
  rt = rt/(bufsz-1);
  rt = rt*ALPHA + buf[bufsz-1]*(1-ALPHA);
  
  return rt;
}
//------------------------------------------------------------------------------
static void _move_filter(void)
{
  disp_rms_u  = _exp_move_avr(buff_rms_u,  BUF_N,  gf_rms_u) *gf_coeff_u;    //V;
  disp_rms_i  = _exp_move_avr(buff_rms_i,  BUF_N,  gf_rms_i) *gf_coeff_i/gu32_turns;   //mA
  disp_pwr_p  = _exp_move_avr(buff_pwr_p,  BUF_N,  gf_pwr_p) *gf_coeff_u *gf_coeff_i/gu32_turns /1000;   //W
  disp_factor = _exp_move_avr(buff_factor, BUF_N,  gf_factor);
  disp_thd    = _exp_move_avr(buff_thd,    BUF_N,  gf_thd);
  
  for(uint32_t i=1; i<=10; i++){
    disp_harmonic_amp[i] = _exp_move_avr(buff_harmonic_amp[i],  BUF_N,  gaf_pt_amp_i[i]) *gf_coeff_i/gu32_turns;
  }
}
//------------------------------------------------------------------------------
static char nbuf[1024];
static void _tx_disp(void)
{
  sprintf(nbuf,
"main.v.txt=\"%0.3f\"\xff\xff\xff\
main.i.txt=\"%0.1f\"\xff\xff\xff\
main.pwr.txt=\"%0.4f\"\xff\xff\xff\
main.factor.txt=\"%0.4f\"\xff\xff\xff\
main.thd.txt=\"%0.3f\"\xff\xff\xff\
main.tb.txt=\"%0.3f\"\xff\xff\xff\
main.t2.txt=\"%0.3f\"\xff\xff\xff\
main.t3.txt=\"%0.3f\"\xff\xff\xff\
main.t4.txt=\"%0.3f\"\xff\xff\xff\
main.t5.txt=\"%0.3f\"\xff\xff\xff\
main.t6.txt=\"%0.3f\"\xff\xff\xff\
main.t7.txt=\"%0.3f\"\xff\xff\xff\
main.t8.txt=\"%0.3f\"\xff\xff\xff\
main.t9.txt=\"%0.3f\"\xff\xff\xff\
main.t10.txt=\"%0.3f\"\xff\xff\xff",
  disp_rms_u,
  disp_rms_i,
  disp_pwr_p,
  disp_factor,
  disp_thd,
  disp_harmonic_amp[1],
  disp_harmonic_amp[2],
  disp_harmonic_amp[3],
  disp_harmonic_amp[4],
  disp_harmonic_amp[5],
  disp_harmonic_amp[6],
  disp_harmonic_amp[7],
  disp_harmonic_amp[8],
  disp_harmonic_amp[9],
  disp_harmonic_amp[10]
  );
  mcu_uart0_dma_tx(nbuf, strlen(nbuf));   //approx 400Bytes
//mcu_uart0_tx(nbuf, strlen(nbuf));
}
//------------------------------------------------------------------------------

//---------------------------------Public Func----------------------------------
int main(void)
{
  calc_init();
  brd_init();
  
  while(1){
    dbg_cyc();
    if(igu32_bufflag){
      igu32_bufflag = 0;
      calc_para();
      _move_filter();
      _tx_disp();
      mcu_led_toggle();     //approx 1024/1600 = 0.64s
      //mcu_delay_ms(1000);
      //mcu_adc_en();
    }
  }
}
//-----------------------------------EOF----------------------------------------





